﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for DataProvider
/// </summary>
public class DataProvider
{
    //KetNoi
    string ChuoiKetNoi = ConfigurationManager.ConnectionStrings["KetNoi"].ConnectionString;
    SqlConnection KetNoi;
    void MoKetNoi()
    {
        KetNoi = new SqlConnection(ChuoiKetNoi);
        KetNoi.Open();
    }
    void DongKetNoi()
    {
        KetNoi.Close();
    }
    //Hàm trả về 1 datatable 
    public DataTable LayDuLieu(string sql, params SqlParameter[] ThamSo)
    {
        MoKetNoi();
        DataTable dtt = new DataTable();
        SqlCommand cmd = new SqlCommand(sql, KetNoi);
        cmd.Parameters.AddRange(ThamSo);
        dtt.Load(cmd.ExecuteReader());
        DongKetNoi();
        return dtt;
    }


    //Cap nhat du lieu
    public void CapNhatDuLieu(string sql, params SqlParameter[] ThamSo)
    {
        MoKetNoi();
        SqlCommand cmd = new SqlCommand(sql, KetNoi);
        cmd.Parameters.AddRange(ThamSo);
        cmd.ExecuteNonQuery();
        DongKetNoi();
    }

    //Lay Du Lieu SP
    public DataTable LayDuLieuSP(string sql, params SqlParameter[] ThamSo)
    {
        MoKetNoi();
        DataTable dtt = new DataTable();
        SqlCommand cmd = new SqlCommand(sql, KetNoi);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddRange(ThamSo);
        dtt.Load(cmd.ExecuteReader());
        DongKetNoi();
        return dtt;
    }
    //Cap nhat du lieu SP
    public void CapNhatDuLieuSP(string sql, params SqlParameter[] ThamSo)
    {
        MoKetNoi();
        SqlCommand cmd = new SqlCommand(sql, KetNoi);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddRange(ThamSo);
        cmd.ExecuteNonQuery();
        DongKetNoi();
    }
}